﻿package com.xingongchang.baolebei.fragment;

import java.util.ArrayList;
import java.util.List;

import org.afinal.simplecache.ACache;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.external.androidquery.callback.AjaxStatus;
import com.external.maxwin.view.XListView;
import com.external.viewpagerindicator.PageIndicator;
import com.insthub.BeeFramework.fragment.BaseFragment;
import com.insthub.BeeFramework.model.BusinessResponse;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.utils.Log;
import com.xingongchang.babyrecord.adapter.BannerPagerAdapter;
import com.xingongchang.baolebei.BaolebeiApp;
import com.xingongchang.baolebei.ImageDisplay;
import com.xingongchang.baolebei.R;
import com.xingongchang.baolebei.activity.ProductDetailActivity;
import com.xingongchang.baolebei.activity.ProductListActivity;
import com.xingongchang.baolebei.activity.TopicDetailActivity;
import com.xingongchang.baolebei.model.HomeModel;
import com.xingongchang.baolebei.model.ProtocolConst;
import com.xingongchang.baolebei.model.SearchModel;
import com.xingongchang.baolebei.model.ShoppingCartModel;
import com.xingongchang.baolebei.protocol.Advert;
import com.xingongchang.baolebei.protocol.FILTER;
import com.xingongchang.baolebei.protocol.Home;
import com.xingongchang.baolebei.protocol.News;
import com.xingongchang.baolebei.protocol.ShouTu;
import com.xingongchang.baolebei.protocol.Tuijian;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @类名:HomeFragment
 * @描述:TODO(左右滑动) implements BusinessResponse, RegisterApp, OnClickListener
 */
public class HomeFragment extends BaseFragment implements BusinessResponse,
		OnClickListener, XListView.IXListViewListener
{
	private View view;
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	List<ShouTu> list;
	private HomeModel homeModel;
	// 图片
	public ArrayList<ShouTu> shuotu = new ArrayList<ShouTu>();
	public ArrayList<Tuijian> tuijians = new ArrayList<Tuijian>();
	public ArrayList<News> newShopping = new ArrayList<News>();
	public ArrayList<Advert> advertd = new ArrayList<Advert>();
	public ArrayList<Home> hotShoppping = new ArrayList<Home>();
	// 推荐
	private TextView view1, view2, view3;
	private ImageView imageView1, imageView2, imageView3;
	// 速递
	private TextView view4, view5, view6, view7, view8, view9;
	private ImageView imageView4, imageView5, imageView6, imageView7,
			imageView8, imageView9;
	// 热门u
	private TextView view10, view11, view12, view13, view14, view15;
	private ImageView im4, im5, im6, im7, im8, im9;
	ImageView image;
	// 图片
	private ViewPager bannerViewPager;
	private ArrayList<View> bannerViews;
	private PageIndicator mIndicator;
	int count = 0;
	int total = 0;
	BannerPagerAdapter bannerPagerAdapter;
	TextView text, text2, text3;
	// 点击
	LinearLayout layout1, layout2, layout3, layout5, layout6, layout7, layout8,
			layout9, layout11, layout12, layout13, layout14, layout15, layout4,
			layout10, img_lin, abc;

	private SearchModel searchModel;
	DisplayImageOptions options;
	// 更多
	private TextView tuijian, sudi, remen;
	// 新品与热卖中间的图片
	private ImageView commtent_img;
	int mScreenH;
	int mScreenW;
	private XListView listView;
	private View heaView;
	ShoppingCartModel shoppingCartModel = null;
	boolean isfalse = false;
	boolean istrue = false;
	ACache Cache;// 缓存
	NetworkInfo mNetworkInfo;// 判断网络是否连接
	ConnectivityManager mConnectivityManager ;
	Handler handler = new Handler()
	{
		public void handleMessage(android.os.Message msg)
		{
		};
	};

	private void findview()
	{
		// 推荐
		view1 = (TextView) heaView.findViewById(R.id.imgText1);
		view2 = (TextView) heaView.findViewById(R.id.imgText2);
		view3 = (TextView) heaView.findViewById(R.id.imgtext3);
		imageView1 = (ImageView) heaView.findViewById(R.id.img1);
		imageView2 = (ImageView) heaView.findViewById(R.id.img2);
		imageView3 = (ImageView) heaView.findViewById(R.id.img3);
		// 速递
		view4 = (TextView) heaView.findViewById(R.id.igtext1);
		view5 = (TextView) heaView.findViewById(R.id.igtext2);
		view6 = (TextView) heaView.findViewById(R.id.igtext3);
		view7 = (TextView) heaView.findViewById(R.id.igtext4);
		view8 = (TextView) heaView.findViewById(R.id.igtext5);
		view9 = (TextView) heaView.findViewById(R.id.igtext6);
		imageView4 = (ImageView) heaView.findViewById(R.id.ig1);
		imageView5 = (ImageView) heaView.findViewById(R.id.ig2);
		imageView6 = (ImageView) heaView.findViewById(R.id.ig3);
		imageView7 = (ImageView) heaView.findViewById(R.id.ig4);
		imageView8 = (ImageView) heaView.findViewById(R.id.ig5);
		imageView9 = (ImageView) heaView.findViewById(R.id.ig6);
		tuijian = (TextView) heaView.findViewById(R.id.tuijian);
		sudi = (TextView) heaView.findViewById(R.id.sudi);
		remen = (TextView) heaView.findViewById(R.id.remen);
		// 热门
		view10 = (TextView) heaView.findViewById(R.id.gText1);
		view11 = (TextView) heaView.findViewById(R.id.gText2);
		view12 = (TextView) heaView.findViewById(R.id.gText3);
		view13 = (TextView) heaView.findViewById(R.id.gText4);
		view14 = (TextView) heaView.findViewById(R.id.gText5);
		view15 = (TextView) heaView.findViewById(R.id.gText6);
		im4 = (ImageView) heaView.findViewById(R.id.g1);
		im5 = (ImageView) heaView.findViewById(R.id.g2);
		im6 = (ImageView) heaView.findViewById(R.id.g3);
		im7 = (ImageView) heaView.findViewById(R.id.g4);
		im8 = (ImageView) heaView.findViewById(R.id.g5);
		im9 = (ImageView) heaView.findViewById(R.id.g6);
		// 文字
		text = (TextView) heaView.findViewById(R.id.text1);
		text2 = (TextView) heaView.findViewById(R.id.text2);
		text3 = (TextView) heaView.findViewById(R.id.text3);
		// 新品与热卖中间的图片
		commtent_img = (ImageView) heaView.findViewById(R.id.commtent_img);
		layout1 = (LinearLayout) heaView.findViewById(R.id.lin01);
		layout2 = (LinearLayout) heaView.findViewById(R.id.lin02);
		layout3 = (LinearLayout) heaView.findViewById(R.id.lin03);
		layout4 = (LinearLayout) heaView.findViewById(R.id.lin04);
		layout5 = (LinearLayout) heaView.findViewById(R.id.lin05);
		layout6 = (LinearLayout) heaView.findViewById(R.id.lin06);
		layout7 = (LinearLayout) heaView.findViewById(R.id.lin07);
		layout8 = (LinearLayout) heaView.findViewById(R.id.lin08);
		layout9 = (LinearLayout) heaView.findViewById(R.id.lin09);
		layout10 = (LinearLayout) heaView.findViewById(R.id.lin1);
		layout11 = (LinearLayout) heaView.findViewById(R.id.lin2);
		layout12 = (LinearLayout) heaView.findViewById(R.id.lin3);
		layout13 = (LinearLayout) heaView.findViewById(R.id.lin4);
		layout14 = (LinearLayout) heaView.findViewById(R.id.lin5);
		layout15 = (LinearLayout) heaView.findViewById(R.id.lin6);
		img_lin = (LinearLayout) heaView.findViewById(R.id.img_lin);
		abc = (LinearLayout) view.findViewById(R.id.abc);
	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

	}

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		view = inflater.inflate(R.layout.home_tabs, null);
		imageLoader.init(ImageLoaderConfiguration.createDefault(getActivity()));
		// 缓存
		Cache = ACache.get(getActivity());
		// 判断是否连接网络
		 mConnectivityManager = (ConnectivityManager) getActivity()
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.default_image)
				.showImageForEmptyUri(R.drawable.default_image)
				.showImageOnFail(R.drawable.default_image).cacheInMemory(true)
				.cacheOnDisk(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
		heaView = inflater.inflate(R.layout.home_tabs_head, null);
		listView = (XListView) view.findViewById(R.id.home_tabs_list);
		listView.setPullLoadEnable(false);// 有加载
		listView.setXListViewListener(this, 1);
		listView.setPullRefreshEnable(true);// 无加载更多
		listView.addHeaderView(heaView);
		listView.setAdapter(null);
		findview();
		bannerViewPager = (ViewPager) heaView.findViewById(R.id.kui);
		android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) bannerViewPager
				.getLayoutParams();
		params.width = getDisplayMetricsWidth();
		params.height = (int) (params.width * 1.0 / 32 * 13);
		bannerViewPager.setLayoutParams(params);
		mIndicator = (PageIndicator) heaView.findViewById(R.id.intss);
		homeModel = new HomeModel(getActivity());
		homeModel.addResponseListener(this);
		if (mConnectivityManager.getActiveNetworkInfo() != null && mConnectivityManager.getActiveNetworkInfo().isConnected())
		{
			if (mNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI
					|| mNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
			{

				homeModel.tupian();
				shoppingCartModel = new ShoppingCartModel(getActivity());
				shoppingCartModel.addResponseListener(this);
				shoppingCartModel.homeCartList();
				searchModel = new SearchModel(getActivity());
				searchModel.searchCategory();
				bannerViews = new ArrayList<View>();
				bannerPagerAdapter = new BannerPagerAdapter(bannerViews);
				bannerViewPager.setAdapter(bannerPagerAdapter);
				bannerViewPager.setCurrentItem(0);
				mIndicator.setViewPager(bannerViewPager);
				addBannerView();
			}
		}
		bannerViews = new ArrayList<View>();
		bannerPagerAdapter = new BannerPagerAdapter(bannerViews);
		bannerViewPager.setAdapter(bannerPagerAdapter);
		bannerViewPager.setCurrentItem(0);
		mIndicator.setViewPager(bannerViewPager);
		DisplayMetrics dm = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
		mScreenW = dm.widthPixels; // 得到宽度
		mScreenH = dm.heightPixels; // 得到高度
		int widths = getDisplayMetricsWidth();
		android.widget.LinearLayout.LayoutParams param3 = (LayoutParams) img_lin
				.getLayoutParams();
		param3.width = widths;
		param3.height = (int) (params.width * 1.0 / 32 * 13);
		commtent_img.setLayoutParams(param3);
		CacheString();
		return view;
	}

	public void CacheString()
	{
		if (mConnectivityManager.getActiveNetworkInfo() == null)
		{
			// 首页图片
			JSONArray jsonArray = Cache.getAsJSONArray("array");
			Log.v("abc", jsonArray.toString());
			if (null != jsonArray && jsonArray.length() > 0)
			{
				shuotu.clear();
				for (int i = 0; i < jsonArray.length(); i++)
				{
					JSONObject playerJsonObject;
					try
					{
						playerJsonObject = jsonArray.getJSONObject(i);
						ShouTu playItem = ShouTu.fromJson(playerJsonObject);
						shuotu.add(playItem);
					} catch (JSONException e)
					{
						e.printStackTrace();
					}
				}
			}
			total = shuotu.size();
			count = 0;
			bannerViews.clear();
			for (int i = 0; i < shuotu.size(); i++)
			{
				final ShouTu ad = shuotu.get(i);
				final ImageView iamgeView = new ImageView(getActivity());
				iamgeView.setScaleType(ScaleType.CENTER_CROP);
				imageLoader.displayImage(ad.showImg, iamgeView, options);
				Log.v("simonpan", "缓存" + ad.showImg);
				iamgeView.setTag(ad);
				bannerViews.add(iamgeView);
				iamgeView.setOnClickListener(new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						ShouTu ad = (ShouTu) v.getTag();
						if (ad.is_goods == 1)
						{
							Intent intent = new Intent(getActivity(),
									ProductDetailActivity.class);
							intent.putExtra("good_id", ad.mid);
							startActivity(intent);
						} else
						{
							Intent intent = new Intent(getActivity(),
									TopicDetailActivity.class);
							intent.putExtra("topicid", ad.mid);
							startActivity(intent);
						}
					}
				});
			}
			mIndicator.notifyDataSetChanged();
			bannerPagerAdapter.mListViews = bannerViews;
			bannerViewPager.setAdapter(bannerPagerAdapter);
			bannerViewPager.setCurrentItem(count);
			handler.removeCallbacks(runnable);
			handler.postDelayed(runnable, 5000);
			// 首页推荐
			JSONArray array = Cache.getAsJSONArray("recommens");
			Log.v("abc", "推荐" + array.toString());
			if (null != array && array.length() > 0)
			{
				tuijians.clear();
				for (int i = 0; i < array.length(); i++)
				{
					try
					{
						JSONObject playerJsonObject = array.getJSONObject(i);
						Tuijian playItem = Tuijian.fromJson(playerJsonObject);
						tuijians.add(playItem);
					} catch (JSONException e)
					{
						e.printStackTrace();
					}
				}
			}
			view1.setText(tuijians.get(0).goods_name.toString());
			imageLoader.displayImage(tuijians.get(0).goods_img, imageView1,
					options);
			view2.setText(tuijians.get(1).goods_name.toString());
			imageLoader.displayImage(tuijians.get(1).goods_img, imageView2,
					options);
			view3.setText(tuijians.get(2).goods_name.toString());
			imageLoader.displayImage(tuijians.get(2).goods_img, imageView3,
					options);

			// 首页新品
			JSONArray array11 = Cache.getAsJSONArray("news");
			Log.v("abc", "新品" + array11.toString());
			if (null != array11 && array11.length() > 0)
			{
				newShopping.clear();
				for (int i = 0; i < array11.length(); i++)
				{
					JSONObject playerJsonObject;
					try
					{
						playerJsonObject = array11.getJSONObject(i);
						News playItem = News.fromJson(playerJsonObject);
						newShopping.add(playItem);
					} catch (JSONException e)
					{
						e.printStackTrace();
					}
				}
			}
			view5.setText(newShopping.get(0).goodName.toString());
			ImageDisplay.show(getActivity(), newShopping.get(0).goodImg,
					imageView5);
			view6.setText(newShopping.get(1).goodName.toString());
			ImageDisplay.show(getActivity(), newShopping.get(1).goodImg,
					imageView6);
			view7.setText(newShopping.get(2).goodName.toString());
			ImageDisplay.show(getActivity(), newShopping.get(2).goodImg,
					imageView7);
			view8.setText(newShopping.get(3).goodName.toString());
			ImageDisplay.show(getActivity(), newShopping.get(3).goodImg,
					imageView8);
			view9.setText(newShopping.get(4).goodName.toString());
			ImageDisplay.show(getActivity(), newShopping.get(4).goodImg,
					imageView9);
			// 首页热门
			JSONArray array2 = Cache.getAsJSONArray("hots");
			Log.v("abc", "热门" + array2.toString());
			if (null != array2 && array2.length() > 0)
			{
				hotShoppping.clear();
				for (int i = 0; i < array2.length(); i++)
				{
					JSONObject playerJsonObject;
					try
					{
						playerJsonObject = array2.getJSONObject(i);
						Home playItem = Home.fromJson(playerJsonObject);
						hotShoppping.add(playItem);
					} catch (JSONException e)
					{
						e.printStackTrace();
					}
				}
			}
			view11.setText(hotShoppping.get(0).g_name.toString());
			ImageDisplay.show(getActivity(), hotShoppping.get(0).g_img, im5);
			view12.setText(hotShoppping.get(1).g_name.toString());
			ImageDisplay.show(getActivity(), hotShoppping.get(1).g_img, im6);
			view13.setText(hotShoppping.get(2).g_name.toString());
			ImageDisplay.show(getActivity(), hotShoppping.get(2).g_img, im7);
			view14.setText(hotShoppping.get(3).g_name.toString());
			ImageDisplay.show(getActivity(), hotShoppping.get(3).g_img, im8);
			imageLoader.displayImage(hotShoppping.get(3).g_img, im8,
					BaolebeiApp.options);
			view15.setText(hotShoppping.get(4).g_name.toString());
			ImageDisplay.show(getActivity(), hotShoppping.get(4).g_img, im9);
			// 其他功能
			JSONArray array3 = Cache.getAsJSONArray("other_ads");
			Log.v("abc", "其他" + array3.toString());
			if (null != array3 && array3.length() > 0)
			{
				advertd.clear();
				for (int i = 0; i < array3.length(); i++)
				{
					JSONArray playerJSONArray;
					try
					{
						playerJSONArray = array3.getJSONArray(i);
						if (playerJSONArray.length() > 0)
						{
							Advert playItem = Advert.fromJson(playerJSONArray
									.getJSONObject(0));
							advertd.add(playItem);
						}
					} catch (JSONException e)
					{
						e.printStackTrace();
					}
				}
			}
			view10.setVisibility(View.GONE);
			ImageDisplay.show(getActivity(), advertd.get(1).show_img, im4);
			ImageDisplay.show(getActivity(), advertd.get(0).show_img,
					imageView4);
			if (advertd.get(2) == null)
			{
				commtent_img.setVisibility(View.GONE);
			} else
			{
				commtent_img.setVisibility(View.VISIBLE);
				ImageDisplay.show(getActivity(), advertd.get(2).show_img,
						commtent_img);
				commtent_img.setOnClickListener(new OnClickListener()
				{
					@Override
					public void onClick(View arg0)
					{
						Intent intent = new Intent(getActivity(),
								TopicDetailActivity.class);
						startActivity(intent);
					}
				});
			}
			layout1.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View arg0)
				{
					Intent intent1 = new Intent(getActivity(),
							ProductDetailActivity.class);
					intent1.putExtra("good_id",tuijians.get(0).good);
					startActivity(intent1);
				}
			});
			layout2.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View arg0)
				{
					Intent intent1 = new Intent(getActivity(),
							ProductDetailActivity.class);
					intent1.putExtra("good_id",tuijians.get(1).good);
					startActivity(intent1);
				}
			});
			layout3.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View arg0)
				{
					Intent intent1 = new Intent(getActivity(),
							ProductDetailActivity.class);
					intent1.putExtra("good_id",tuijians.get(2).good);
					startActivity(intent1);
				}
			});
			layout4.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View arg0)
				{
					Intent intent1 = new Intent(getActivity(),
							ProductDetailActivity.class);
					intent1.putExtra("good_id",advertd.get(1).id);
					startActivity(intent1);
				}
			});
			layout5.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View arg0)
				{
					Intent intent1 = new Intent(getActivity(),
							ProductDetailActivity.class);
					intent1.putExtra("good_id",newShopping.get(0).goodId);
					startActivity(intent1);
				}
			});
			layout6.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View arg0)
				{
					Intent intent1 = new Intent(getActivity(),
							ProductDetailActivity.class);
					intent1.putExtra("good_id",newShopping.get(1).goodId);
					startActivity(intent1);
				}
			});
			layout7.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View arg0)
				{
					Intent intent1 = new Intent(getActivity(),
							ProductDetailActivity.class);
					intent1.putExtra("good_id",newShopping.get(2).goodId);
					startActivity(intent1);
				}
			});
			layout8.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View arg0)
				{
					Intent intent1 = new Intent(getActivity(),
							ProductDetailActivity.class);
					intent1.putExtra("good_id",newShopping.get(3).goodId);
					startActivity(intent1);
				}
			});
			layout9.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View arg0)
				{
					Intent intent1 = new Intent(getActivity(),
							ProductDetailActivity.class);
					intent1.putExtra("good_id",newShopping.get(4).goodId);
					startActivity(intent1);
				}
			});
			layout10.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View arg0)
				{
					Intent intent1 = new Intent(getActivity(),
							ProductDetailActivity.class);
					intent1.putExtra("good_id", advertd.get(1).id);
					startActivity(intent1);
				}
			});
			layout11.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View arg0)
				{
					Intent intent1 = new Intent(getActivity(),
							ProductDetailActivity.class);
					intent1.putExtra("good_id",hotShoppping.get(0).g_id);
					startActivity(intent1);
				}
			});
			layout12.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View arg0)
				{
					Intent intent1 = new Intent(getActivity(),
							ProductDetailActivity.class);
					intent1.putExtra("good_id",hotShoppping.get(1).g_id);
					startActivity(intent1);
				}
			});
			layout13.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View arg0)
				{
					Intent intent1 = new Intent(getActivity(),
							ProductDetailActivity.class);
					intent1.putExtra("good_id",hotShoppping.get(2).g_id);
					startActivity(intent1);
				}
			});
			layout14.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View arg0)
				{
					Intent intent1 = new Intent(getActivity(),
							ProductDetailActivity.class);
					intent1.putExtra("good_id",hotShoppping.get(3).g_id);
					startActivity(intent1);
				}
			});
			layout15.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View arg0)
				{
					Intent intent1 = new Intent(getActivity(),
							ProductDetailActivity.class);
					intent1.putExtra("good_id",hotShoppping.get(4).g_id);
					startActivity(intent1);
				}
			});

		}
	}

	public void addBannerView()
	{
		total = homeModel.viewp.size();
		count = 0;
		bannerViews.clear();
		for (int i = 0; i < homeModel.viewp.size(); i++)
		{
			final ShouTu ad = homeModel.viewp.get(i);
			final ImageView iamgeView = new ImageView(getActivity());
			iamgeView.setScaleType(ScaleType.CENTER_CROP);
			ImageDisplay.show(getActivity(), ad.showImg, iamgeView);
			Log.v("abc", ad.showImg);
			iamgeView.setTag(ad);
			bannerViews.add(iamgeView);

			iamgeView.setOnClickListener(new OnClickListener()
			{

				@Override
				public void onClick(View v)
				{
					// TODO Auto-generated method stub
					ShouTu ad = (ShouTu) v.getTag();
					if (ad.is_goods == 1)
					{
						Intent intent = new Intent(getActivity(),
								ProductDetailActivity.class);
						intent.putExtra("good_id", ad.mid);
						startActivity(intent);
					} else
					{
						Intent intent = new Intent(getActivity(),
								TopicDetailActivity.class);
						intent.putExtra("topicid", ad.mid);
						startActivity(intent);
					}
				}
			});

		}

		mIndicator.notifyDataSetChanged();
		bannerPagerAdapter.mListViews = bannerViews;
		bannerViewPager.setAdapter(bannerPagerAdapter);
		bannerViewPager.setCurrentItem(count);
		handler.removeCallbacks(runnable);
		handler.postDelayed(runnable, 5000);

	}

	Runnable runnable = new Runnable()
	{
		@Override
		public void run()
		{
			count++;
			if (total == count)
			{
				count = 0;
			}
			bannerViewPager.setCurrentItem(count);
			handler.postDelayed(this, 5000);
		}
	};

	// 获取屏幕宽度
	public int getDisplayMetricsWidth()
	{
		int i = getActivity().getWindowManager().getDefaultDisplay().getWidth();
		int j = getActivity().getWindowManager().getDefaultDisplay()
				.getHeight();
		return Math.min(i, j);
	}

	@Override
	public void onClick(View v)
	{

		Intent it = new Intent(getActivity(), ProductListActivity.class);

		switch (v.getId())
		{
		case R.id.tuijian:
			try
			{
				FILTER filter = new FILTER();
				filter.sort_by = "is_best";
				it.putExtra(ProductListActivity.FILTER, filter.toJson()
						.toString());
				it.putExtra("name", text.getText().toString());
				startActivity(it);
				getActivity().overridePendingTransition(R.anim.push_right_in,
						R.anim.push_right_out);
			} catch (JSONException e)
			{
				e.printStackTrace();
			}
			break;

		case R.id.sudi:

			try
			{
				FILTER filter = new FILTER();
				filter.sort_by = "is_new";
				it.putExtra(ProductListActivity.FILTER, filter.toJson()
						.toString());
				it.putExtra("name", text2.getText().toString());
				startActivity(it);
				getActivity().overridePendingTransition(R.anim.push_right_in,
						R.anim.push_right_out);
			} catch (JSONException e)
			{
				e.printStackTrace();
			}
			break;

		case R.id.remen:
			try
			{
				FILTER filter = new FILTER();
				filter.sort_by = "is_hot";
				it.putExtra(ProductListActivity.FILTER, filter.toJson()
						.toString());
				it.putExtra("name", text3.getText().toString());
				startActivity(it);
				getActivity().overridePendingTransition(R.anim.push_right_in,
						R.anim.push_right_out);
			} catch (JSONException e)
			{
				e.printStackTrace();
			}
			break;
		case R.id.lin01:
			Intent intent1 = new Intent(getActivity(),
					ProductDetailActivity.class);
			intent1.putExtra("good_id", homeModel.zuijian.get(0).good);
			startActivity(intent1);
			break;
		case R.id.lin02:
			Intent intent2 = new Intent(getActivity(),
					ProductDetailActivity.class);
			intent2.putExtra("good_id", homeModel.zuijian.get(1).good);
			startActivity(intent2);
			break;
		case R.id.lin03:
			Intent intent3 = new Intent(getActivity(),
					ProductDetailActivity.class);
			intent3.putExtra("good_id", homeModel.zuijian.get(2).good);
			startActivity(intent3);
			break;
		case R.id.lin04:
			Intent intentw = new Intent(getActivity(),
					ProductDetailActivity.class);
			intentw.putExtra("good_id", homeModel.adverts.get(0).id);
			startActivity(intentw);
			break;
		case R.id.lin05:
			Intent intent4 = new Intent(getActivity(),
					ProductDetailActivity.class);
			intent4.putExtra("good_id", homeModel.shudao.get(0).goodId);
			startActivity(intent4);
			break;
		case R.id.lin06:
			Intent intent8 = new Intent(getActivity(),
					ProductDetailActivity.class);
			intent8.putExtra("good_id", homeModel.shudao.get(1).goodId);
			startActivity(intent8);
			break;
		case R.id.lin07:
			Intent intent7 = new Intent(getActivity(),
					ProductDetailActivity.class);
			intent7.putExtra("good_id", homeModel.shudao.get(2).goodId);
			startActivity(intent7);
			break;
		case R.id.lin08:
			Intent intent9 = new Intent(getActivity(),
					ProductDetailActivity.class);
			intent9.putExtra("good_id", homeModel.shudao.get(3).goodId);
			startActivity(intent9);
			break;
		case R.id.lin09:
			Intent intent10 = new Intent(getActivity(),
					ProductDetailActivity.class);
			intent10.putExtra("good_id", homeModel.shudao.get(4).goodId);
			startActivity(intent10);
			break;
		case R.id.lin1:
			Intent intent = new Intent(getActivity(),
					ProductDetailActivity.class);
			intent.putExtra("good_id", homeModel.adverts.get(1).id);
			startActivity(intent);
			break;
		case R.id.lin2:
			Intent intent11 = new Intent(getActivity(),
					ProductDetailActivity.class);
			intent11.putExtra("good_id", homeModel.zuire.get(0).g_id);
			startActivity(intent11);
			break;
		case R.id.lin3:
			Intent intent12 = new Intent(getActivity(),
					ProductDetailActivity.class);
			intent12.putExtra("good_id", homeModel.zuire.get(1).g_id);
			startActivity(intent12);
			break;
		case R.id.lin4:
			Intent intent13 = new Intent(getActivity(),
					ProductDetailActivity.class);
			intent13.putExtra("good_id", homeModel.zuire.get(2).g_id);
			startActivity(intent13);
			break;
		case R.id.lin5:
			Intent intent14 = new Intent(getActivity(),
					ProductDetailActivity.class);
			intent14.putExtra("good_id", homeModel.zuire.get(3).g_id);
			startActivity(intent14);
			break;
		case R.id.lin6:
			Intent intent15 = new Intent(getActivity(),
					ProductDetailActivity.class);
			intent15.putExtra("good_id", homeModel.zuire.get(4).g_id);
			startActivity(intent15);
			break;
		}
	}

	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
	{
		if (url.endsWith(ProtocolConst.SHOUYE))
		{
			if (homeModel.responseStatus.succeed == 1)
			{
				addBannerView();
				SHOUTU();
				SHOUSONG();
				ZUIRE();
				Adverts();
				homeModel.pd.dismiss();
				layout1.setOnClickListener(this);
				layout2.setOnClickListener(this);
				layout3.setOnClickListener(this);
				layout4.setOnClickListener(this);
				layout5.setOnClickListener(this);
				layout6.setOnClickListener(this);
				layout7.setOnClickListener(this);
				layout8.setOnClickListener(this);
				layout9.setOnClickListener(this);
				layout10.setOnClickListener(this);
				layout11.setOnClickListener(this);
				layout12.setOnClickListener(this);
				layout13.setOnClickListener(this);
				layout14.setOnClickListener(this);
				layout15.setOnClickListener(this);
				tuijian.setOnClickListener(this);
				sudi.setOnClickListener(this);
				remen.setOnClickListener(this);
			}
		} else if (url.endsWith(ProtocolConst.CARTLIST))
		{
			TabsFragment.setShoppingcartNum();
		}
	}

	private void Adverts()
	{
		view10.setVisibility(View.GONE);
		ImageDisplay
				.show(getActivity(), homeModel.adverts.get(1).show_img, im4);
		ImageDisplay.show(getActivity(), homeModel.adverts.get(0).show_img,
				imageView4);
		if (homeModel.adverts.get(2) == null)
		{
			commtent_img.setVisibility(View.GONE);
		} else
		{
			commtent_img.setVisibility(View.VISIBLE);
			ImageDisplay.show(getActivity(), homeModel.adverts.get(2).show_img,
					commtent_img);
			commtent_img.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View arg0)
				{
					Intent intent = new Intent(getActivity(),
							TopicDetailActivity.class);
					intent.putExtra("topicid", homeModel.adverts.get(2).id);
					startActivity(intent);
				}
			});
		}
		android.widget.LinearLayout.LayoutParams param3 = (LayoutParams) abc
				.getLayoutParams();
		param3.width = mScreenW * 2 / 3;
		param3.height = (int) (param3.width * 1.0 / 11 * 11);
		im4.setLayoutParams(param3);

		view4.setVisibility(View.GONE);
		android.widget.LinearLayout.LayoutParams param2 = (LayoutParams) abc
				.getLayoutParams();
		param2.width = mScreenW * 2 / 3;
		param2.height = (int) (param2.width * 1.0 / 11 * 11);
		imageView4.setLayoutParams(param2);

	}

	private void ZUIRE()
	{
		view11.setText(homeModel.zuire.get(0).g_name.toString());
		ImageDisplay.show(getActivity(), homeModel.zuire.get(0).g_img, im5);
		// Toast.makeText(getActivity(), ""+homeModel.zuire.get(0).g_img,
		// 0).show();
		Log.v("simonpan", homeModel.zuire.get(0).g_img);
		view12.setText(homeModel.zuire.get(1).g_name.toString());
		ImageDisplay.show(getActivity(), homeModel.zuire.get(1).g_img, im6);
		view13.setText(homeModel.zuire.get(2).g_name.toString());
		ImageDisplay.show(getActivity(), homeModel.zuire.get(2).g_img, im7);
		view14.setText(homeModel.zuire.get(3).g_name.toString());
		ImageDisplay.show(getActivity(), homeModel.zuire.get(3).g_img, im8);
		imageLoader.displayImage(homeModel.zuire.get(3).g_img, im8,
				BaolebeiApp.options);
		view15.setText(homeModel.zuire.get(4).g_name.toString());
		ImageDisplay.show(getActivity(), homeModel.zuire.get(4).g_img, im9);
	}

	private void SHOUSONG()
	{
		view5.setText(homeModel.shudao.get(0).goodName.toString());
		ImageDisplay.show(getActivity(), homeModel.shudao.get(0).goodImg,
				imageView5);
		view6.setText(homeModel.shudao.get(1).goodName.toString());
		ImageDisplay.show(getActivity(), homeModel.shudao.get(1).goodImg,
				imageView6);
		view7.setText(homeModel.shudao.get(2).goodName.toString());
		ImageDisplay.show(getActivity(), homeModel.shudao.get(2).goodImg,
				imageView7);
		view8.setText(homeModel.shudao.get(3).goodName.toString());
		ImageDisplay.show(getActivity(), homeModel.shudao.get(3).goodImg,
				imageView8);
		view9.setText(homeModel.shudao.get(4).goodName.toString());
		ImageDisplay.show(getActivity(), homeModel.shudao.get(4).goodImg,
				imageView9);
	}

	private void SHOUTU()
	{
		view1.setText(homeModel.zuijian.get(0).goods_name.toString());
		ImageDisplay.show(getActivity(), homeModel.zuijian.get(0).goods_img,
				imageView1);
		view2.setText(homeModel.zuijian.get(1).goods_name.toString());
		ImageDisplay.show(getActivity(), homeModel.zuijian.get(1).goods_img,
				imageView2);
		view3.setText(homeModel.zuijian.get(2).goods_name.toString());
		ImageDisplay.show(getActivity(), homeModel.zuijian.get(2).goods_img,
				imageView3);
	}

	@Override
	public void onDestroy()
	{
		super.onDestroy();
		homeModel.removeResponseListener(this);
	}

	@Override
	public void onPause()
	{
		super.onPause();
		MobclickAgent.onPageEnd("Home");
	}

	@Override
	public void onResume()
	{
		super.onResume();
		mConnectivityManager = (ConnectivityManager) getActivity()
				.getSystemService(Context.CONNECTIVITY_SERVICE);
	mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
	if(mNetworkInfo != null && mNetworkInfo.isConnected()){
	if (mNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI
			|| mNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
		{
			homeModel.tupian();
		}
	}
	}

	@Override
	public void onRefresh(int id)
	{
		mConnectivityManager = (ConnectivityManager) getActivity()
					.getSystemService(Context.CONNECTIVITY_SERVICE);
		mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
		if(mNetworkInfo != null && mNetworkInfo.isConnected()){
		if (mNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI
				|| mNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
			{
				homeModel.tupian(); 
			}
		}
	}

	@Override
	public void onLoadMore(int id)
	{

	}

}
